import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {

  var methodChannel: FlutterMethodChannel?
    
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)
      
      if #available(iOS 10.0, *) {
        UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
      }
      let flutterVC = window.rootViewController as? FlutterViewController
      let navigationController = UINavigationController(rootViewController: flutterVC!)
      navigationController.isNavigationBarHidden = true

      self.window = UIWindow(frame: UIScreen.main.bounds)
      self.window.rootViewController = navigationController
      self.window.makeKeyAndVisible()
      self.window.windowLevel = .alert
      
      let adPln = adPlugin()
      adPln.adStart()
      
      methodChannel = FlutterMethodChannel(name: "defaultChannel", binaryMessenger: flutterVC as! FlutterBinaryMessenger)
      methodChannel?.setMethodCallHandler{call,result in
          if(call.method == "checkin"){
              adPln.adLoad(call.method)
          } else if(call.method.starts(with: "{\"route")) {
              adPln.adLoad(call.method)
          } else if(call.method == "checkinPlan"){
              result(UserDefaults.standard.value(forKey: "checkinPlan"))
          }
      }
      
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}
